define([ "app", "jquery", "md5", "ecb","artDialog","iframeTools", "pagination", "services/UserService","services/TipsService",
		"directives/AudioPlayer",
		"resources/company/CompanyResource", 
		"resources/UserResource", "resources/ShareResource" ],
		function(app) {
			var deps = [ "$scope", "$state","$sce", "$timeout", "$window", "$location", "$stateParams","TipsService", "UserService", "UserResource", "ShareResource","CompanyResource" ];

			function controller($scope, $state,$sce,$timeout, $window, $location, $stateParams,TipsService, UserService, UserResource, ShareResource,CompanyResource) {
				
				$scope.selectedUser = {}; // 点击后保存的单个用户简历
				$scope.selectedUserList = {}; // 点击后保存的用户简历列表
				$scope.isSelectAll = false;
				
				$scope.workYearType="";
				// 初始化加载数据
				$scope.currentPage = 1;
		        $scope.pages = [];
		        $scope.pageSize = 10;
		        //$scope.selected=10;
		        queryData();
		        
		        // 收藏简历
				$scope.collectionResume = function() {
					var ids=convertMapToArray($scope.selectedUserList)
					if(ids.length>0){
						CompanyResource.collectionResume(ids).success(function(data){
							// 置空
							$scope.selectedUserList = {};
							queryData();
			        		$scope.isSelectAll = false;
			            });
					}
				};
				/**
		    	 * 对象转列表 .
		    	 */
		    	function convertMapToArray(map) {
		    		var array = [];
		    		for(var i in map) {
		    			array.push(map[i].resumeId);
		    		}
		    		return array;
		    	}
		        
		        
		        // 选择所有用户
				$scope.toggleAll = function() {
					if ($scope.isSelectAll) {
						$scope.selectedUser = {}; // 点击后保存的单个用户
						$scope.selectedUserList = {}; // 点击后保存的用户列表
						$scope.isSelectAll = false;
					} else {
						for (var i = 0; i < $scope.data.length; i++) {
							$scope.selectedUserList[$scope.data[i].resumeId] = $scope.data[i];
						}
						$scope.isSelectAll = true;
					}
				};
		        
		        // 选择一个用户
				$scope.toggleUser = function(record) {
					$scope.selectedUser = record;
					if($scope.selectedUserList[record.resumeId]) {
						delete $scope.selectedUserList[record.resumeId];
					} else {
						$scope.selectedUserList[record.resumeId] = record;
					}
				};
				
				
		        function queryData(){
		        	CompanyResource.queryResumeList($scope.currentPage,$scope.pageSize,$scope.keyWord,$scope.workYearType).success(function(data){
		        		if(data.obj==null){
							return;
						}
		        		if(data.obj.count>1){
		        			$scope.currentPage=1;
		        		}else{
		        			$scope.currentPage=0;
		        		}
		        		$scope.data = data.obj.resumeList;
						if(data.obj.count % $scope.pageSize == 0){
		         			$scope.totalPage=Math.floor(data.obj.count/$scope.pageSize);
		        		}else{
		        			$scope.totalPage=Math.floor(data.obj.count/$scope.pageSize)+1;
		        		}
						
						var obj = {
							    wrapid:'wrap1', //页面显示分页器容器id
							    total:data.obj.count,//总条数
							    pagesize:10,//每页显示10条
							    currentPage:1,//当前页
							    onPagechange:onPagechange,
							    //btnCount:7 页数过多时，显示省略号的边界页码按钮数量，可省略，且值是大于5的奇数
						}
						pagination.init(obj);
						
		            });
				}
		        
		        var onPagechange = function(page){
		        	CompanyResource.queryResumeList(page,$scope.pageSize,$scope.keyWord).success(function(data){
		        		if(data.obj==null){
							return;
						}
		        		if(data.obj.count>1){
		        			$scope.currentPage=1;
		        		}else{
		        			$scope.currentPage=0;
		        		}
		        		$scope.data = data.obj.resumeList;
						if(data.obj.count % $scope.pageSize == 0){
		         			$scope.totalPage=Math.floor(data.obj.count/$scope.pageSize);
		        		}else{
		        			$scope.totalPage=Math.floor(data.obj.count/$scope.pageSize)+1;
		        		}
						
						var obj = {
							    wrapid:'wrap1', //页面显示分页器容器id
							    total:data.obj.count,//总条数
							    pagesize:10,//每页显示10条
							    currentPage:page,//当前页
							    onPagechange:onPagechange,
							    //btnCount:7 页数过多时，显示省略号的边界页码按钮数量，可省略，且值是大于5的奇数
						}
						pagination.init(obj);
						
		            });
				}
		        
		        $scope.queryData= function($event){
		        	queryData();
		        };
		        
		        
		        $scope.checkAll = function (users) {
		        	
		        	if (!$scope.isSelectAll) {
		        		if(users && users.length>0){
		                    for(var i = 0;i<users.length;i++){
		                        var user = users[i];
		                        if(!$scope.selectedNodes[user.resumeId]){
		                            $scope.selectedNodes[user.resumeId] = user;
		                            $scope.nodeCount ++;
		                        }
		                    }
		                    $scope.isSelectAll = true;
		                }
		        		
		        	}else{
		        		
		        		if(users && users.length>0){
		                    for(var i = 0;i<users.length;i++){
		                        var user = users[i];
		                        if($scope.selectedNodes[user.resumeId]){
		                            delete $scope.selectedNodes[user.resumeId];
		                            if( $scope.nodeCount  > 0){
		                                $scope.nodeCount -- ;
		                            }
		                        }
		                    }
		                    $scope.isSelectAll = false;
		                }
		        		
		        	}
		            
		        }
		        
		        // 修改职位
				$scope.viewResume= function(resumeId){
					$state.go('home.viewResume', {
						resumeId:resumeId
	                });
		        };
			}
			controller.$inject = deps;
			return app.lazy.controller("ResumeManagementController", controller);
		});
